/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.activity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.activity.HkShopBargain;
import com.thinkgem.jeesite.modules.hk.shop.dao.activity.HkShopBargainDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.activity.HkShopBargainProduct;
import com.thinkgem.jeesite.modules.hk.shop.dao.activity.HkShopBargainProductDao;

/**
 * 乐购特价Service
 * @author 罗天文
 * @version 2018-05-16
 */
@Service
@Transactional(readOnly = true)
public class HkShopBargainService extends CrudService<HkShopBargainDao, HkShopBargain> {

	@Autowired
	private HkShopBargainProductDao hkShopBargainProductDao;
	
	public HkShopBargain get(String id) {
		HkShopBargain hkShopBargain = super.get(id);
		hkShopBargain.setHkShopBargainProductList(hkShopBargainProductDao.findList(new HkShopBargainProduct(hkShopBargain)));
		return hkShopBargain;
	}
	
	public List<HkShopBargain> findList(HkShopBargain hkShopBargain) {
		return super.findList(hkShopBargain);
	}
	
	public Page<HkShopBargain> findPage(Page<HkShopBargain> page, HkShopBargain hkShopBargain) {
		return super.findPage(page, hkShopBargain);
	}
	
	@Transactional(readOnly = false)
	public void save(HkShopBargain hkShopBargain) {
		super.save(hkShopBargain);
		for (HkShopBargainProduct hkShopBargainProduct : hkShopBargain.getHkShopBargainProductList()){
			if (hkShopBargainProduct.getId() == null){
				continue;
			}
			if (HkShopBargainProduct.DEL_FLAG_NORMAL.equals(hkShopBargainProduct.getDelFlag())){
				if (StringUtils.isBlank(hkShopBargainProduct.getId())){
					hkShopBargainProduct.setBargain(hkShopBargain);
					hkShopBargainProduct.preInsert();
					hkShopBargainProductDao.insert(hkShopBargainProduct);
				}else{
					hkShopBargainProduct.preUpdate();
					hkShopBargainProductDao.update(hkShopBargainProduct);
				}
			}else{
				hkShopBargainProductDao.delete(hkShopBargainProduct);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkShopBargain hkShopBargain) {
		super.delete(hkShopBargain);
		hkShopBargainProductDao.delete(new HkShopBargainProduct(hkShopBargain));
	}
	
}